//
//  AddMasCampViewController.m
//  PartyAnimal
//
//  Created by Hilary Muludiang on 3/17/12.
//  Copyright 2012 Muludiang. All rights reserved.
//

#import "AddMasCampViewController.h"
#import "UIImage+Resize.h"
#import "StackMob.h"

@implementation AddMasCampViewController

@synthesize bandName;
@synthesize carnivalSeason;
@synthesize bandLogoImage,selectLogoButton;
@synthesize bandLogoView;

-(IBAction)cancelSubmit:(UIBarButtonItem *)sender{
	[self dismissModalViewControllerAnimated:YES];
}

-(IBAction)selectYear:(UISegmentedControl *)sender{
	NSLog(@"%@", [carnivalSeason titleForSegmentAtIndex:[carnivalSeason selectedSegmentIndex]]);
}

-(IBAction)selectLogo:(UIButton *)sender{
	UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
	
	imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	imagePicker.delegate = self;
	[self presentModalViewController:imagePicker animated:YES];
	[imagePicker release];
		
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
	[[UIApplication sharedApplication] setStatusBarHidden:NO];
	[self dismissModalViewControllerAnimated:YES];	
	
	bandLogoImage = [[info objectForKey:UIImagePickerControllerOriginalImage] resizedImage:CGSizeMake(320, 460) interpolationQuality:1];
	bandLogoView.image = bandLogoImage;
}

-(IBAction)submitMasCamp:(UIBarButtonItem *)sender{
	NSLog(@"%@", bandLogoImage);
	//Carnivals *carnivals = [Carnivals sharedUser];
	//NSLog(@"%@|%@|%@", [startDate titleLabel].text, endDate.titleLabel.text, country.titleLabel.text);
	if ([[[bandName text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
		UIAlertView *submitAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure all required fields are filled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[submitAlert show];
		[submitAlert release];
	}
	
	else{

		NSData *logo = UIImagePNGRepresentation(bandLogoImage);
		SMFile *logoFile = [SMFile fileWithName:@"logo.png" data:logo contentType:@"image/png"];

		NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys: [[bandName text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]], @"mascampname",
							  [carnivalSeason titleForSegmentAtIndex:[carnivalSeason selectedSegmentIndex]], @"carnivalyear",
							  logoFile, @"mascamplogo", nil];
		//@"hello world!", @"message", @"johndoe", @"author", nil];
		[[StackMob stackmob] post:@"mascamps" withArguments:args andCallback:^(BOOL success, id result) {
			if (success) {
				UIAlertView *submitAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Mas band information had been successfully submitted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
				submitAlert.tag = 1;
				[submitAlert show];
				//[submitAlert release];
				//[self dismissModalViewControllerAnimated:YES];
				
				//NSDictionary *resultObj = (NSDictionary *)result;
				//NSString *itemId = [resultObj objectForKey:@"chatmessage_id"];
				// …
			} else {
				// handle object creation failure
				NSLog(@"Something's wrong....%@", result);
			}
		}];
	}
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (alertView.tag == 1){
	[alertView release];
	[self dismissModalViewControllerAnimated:YES];
}
	
}
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelSubmit:)] autorelease];
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(submitMasCamp:)] autorelease];
	
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	//	[dateFormat setDateFormat:@"yyyy-MM-dd"];
	//	NSDate *tempdate = [dateFormat dateFromString:[carnival objectForKey:@"startDate"]];
	[dateFormat setDateFormat:@"yyyy"];
	NSString *tempStrDate = [dateFormat stringFromDate:[NSDate date]];
	
	NSLog(@"year: %@", tempStrDate);
	
	[dateFormat release];
	
	[carnivalSeason removeAllSegments];
	[carnivalSeason insertSegmentWithTitle:tempStrDate atIndex:0 animated:YES];
	[carnivalSeason insertSegmentWithTitle:[NSString stringWithFormat:@"%i", [tempStrDate intValue] + 1] atIndex:1 animated:YES];
	[carnivalSeason insertSegmentWithTitle:[NSString stringWithFormat:@"%i", [tempStrDate intValue] + 2] atIndex:2 animated:YES];
	
	
	carnivalSeason.selectedSegmentIndex = 0;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textFieldView {
	[textFieldView resignFirstResponder];
	return YES;
}


/*
 -(void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
}*/

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES; // (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[bandLogoImage release];
	[selectLogoButton release];
	[carnivalSeason release];
	[bandName release];
    [super dealloc];
}


@end
